﻿
/*----------------------------------------------------------------
 * 创建时间：2023/9/6 9:37:43
 * 作  者：wenlli 
 * 项  目：Minotaur.App.Modules.Business.Service.SystemLog 
 * 描  述：日志操作类型逻辑接口实现
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时  间：
 * 修改说明：
 * 
 *----------------------------------------------------------------*/


using Mapster;
using Minotaur.App.Core;
using Minotaur.App.Domains.Model.SystemLog;
using Minotaur.App.Domains.Request;
using Minotaur.App.Domains.ViewModel.Logs;
using Minotaur.App.Domains.ViewModel.Table;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minotaur.App.Modules.Business.Service.SystemLog
{
    /// <summary>
    /// 日志操作类型逻辑接口实现
    /// </summary>
    public class SystemLogService : ISystemLogService
    {
        private readonly IDbContext dbContext;
        private readonly GlobalUserModel userModel;

        public SystemLogService(IDbContext dbContext, GlobalUserModel userModel)
        {
            this.dbContext = dbContext;
            this.userModel = userModel;
        }

        public async Task<SystemLogListRespone> IniTableInfos(SearcheRequest searcheRequest)
        {
            SystemLogListRespone respone = new SystemLogListRespone();
            try
            {
                RefAsync<int> total = 0;
                int lan = (int)((userModel.Language + "").ConvertEnumEvent<Domains.Enum.LanguageType>());
                var queryable = this.dbContext.MasterDb.Queryable<SystemRequestLog>();
                if (!string.IsNullOrEmpty(searcheRequest.SearchQuery))
                {
                    queryable = queryable.Where(o => o.CNName.Contains(searcheRequest.SearchQuery) || o.TCnname.Contains(searcheRequest.SearchQuery) || o.ExecuteResult.Contains(searcheRequest.SearchQuery) || o.ExecuteMethod.Contains(searcheRequest.SearchQuery));
                }
                List<SystemLogListViewModel> TableInfoList = await queryable.OrderByDescending(o=>o.AutoId)
                .Select(a => new SystemLogListViewModel()
                {
                    AutoId = a.AutoId,
                    Account = a.Account,
                    CNName = a.CNName,
                    Code = a.Code,
                    ExecuteCompletiontime = a.ExecuteCompletiontime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                    ExecuteDate = a.ExecuteDate.Value.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                    ExecuteMethod = a.ExecuteMethod,
                    ExecuteResult = a.ExecuteResult,
                    LogType = a.LogType,
                    RequestParamtetr = a.RequestParamtetr,
                    TCnname = a.TCnname,
                    UserAgent = a.UserAgent
                }).ToOffsetPageAsync(searcheRequest.PageIndex, searcheRequest.PageSize, total);
                respone.TotalCount = total.Value;
                respone.List = TableInfoList.Adapt<ObservableCollection<SystemLogListViewModel>>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return respone;
        }
    }
}
